const request = require('./request')

const BASE_URL = '/task'

/**
 * 获取广场数据(热门和置顶)
 * @returns 
 */
function getSquare() {
  return request.get(BASE_URL + '/square');
};

/**
 * 获取任务分页
 * @param {*} current 
 * @param {*} size 
 * @returns 
 */
function getTaskPage(current = 1, size = 10) {
  return request.get(BASE_URL + '/page/list', { 
    current, size 
  });
};

/**
 * 获取任务详情
 * @param {*} taskId 
 */
function taskDetail(taskId) {
  return request.get(BASE_URL + '/detail/'+ taskId);
}

/**
 * 加入任务
 * @param {*} taskId 
 */
function joinTask(taskId) {
  // 这里用POST方式，参数放body
  return request.post(BASE_URL + '/join/'+ taskId);
}

/**
 * 退出任务
 * @param {*} taskId 
 */
function quitTask(taskId) {
  return request.delete(BASE_URL + '/quit/' + taskId);
}

/**
 * 删除任务
 * @param {*} taskId 
 */
function delTask(taskId) {
  return request.delete(BASE_URL + '/' + taskId);
}

/**
 * 发布新任务
 * @param {*}  
 */
function publishTask(param) {
  // 这里用POST方式，参数放body
  return request.post(BASE_URL + '/publish', param);
}

/**
 * 编辑任务
 * @param {*} taskId 任务id
 * @param {*} param  参数
 */
function editTask(taskId, param) {
  // 这里用PUT方式，参数放body
  return request.put(BASE_URL + '/edit/'+ taskId, param);
}


/**
 * 获取我的任务分页
 * @param {*} current 
 * @param {*} size 
 * @returns 
 */
function getUserTaskPage(current = 1, size = 10) {
  return request.get(BASE_URL + '/user/task/page/list', { 
    current, size 
  });
};

/**
 * 获取所有的周期规则
 * @param {*} taskId 
 */
function getCycleRuleAll() {
  return request.get(BASE_URL + '/cycle-rule/all');
}

/**
 * 获取任务成员分页
 * @param {*} taskId 
 * @param {*} current 
 * @param {*} size 
 * @returns 
 */
function getTaskMemberPage(taskId = 0, current = 1, size = 10) {
  return request.get(BASE_URL + '/'+ taskId +'/member/page/list', { 
    current, size 
  });
};

/**
 * 获取我的任务详情
 * @param {*} taskId 
 */
function userTaskDetail(userTaskId) {
  return request.get(BASE_URL + '/user/task/detail/'+ userTaskId);
}

/**
 * 我的任务打卡
 * @param {*} userTaskId 
 * @param {*} param
 * @returns 
 */
function userTaskCheckIn(userTaskId, param) {
  return request.post(BASE_URL + '/user/task/'+ userTaskId +'/check-in', param);
};

/**
 * 获取我的任务打卡记录分页
 * @param {*} userTaskId 
 * @param {*} current 
 * @param {*} size 
 * @returns 
 */
function getUserTaskCheckInPage(userTaskId = 0, current = 1, size = 10) {
  return request.get(BASE_URL + '/user/task/'+ userTaskId +'/check-in/page/list', { 
    current, size 
  });
};

/**
 * 分页获取我的任务某天的打卡列表
 * @param {*} userTaskId 
 * @param {*} current 
 * @param {*} size 
 * @param {*} searchKey 
 * @returns 
 */
function getUserTaskCheckInPageListForDate(checkInDate = undefined, state = undefined, current = 1, size = 10, searchKey) {
  return request.get(BASE_URL + '/check-in/'+ checkInDate +'/'+ state +'/page/list', { 
    current, size , searchKey
  });
};

/**
 * 提前结束任务
 * @param {*}  
 */
function earlyFinishTask(taskId) {
  // 这里用POST方式，参数放body
  return request.post(BASE_URL + '/early/finish/'+ taskId);
}

/**
 * 获取任务搜索分页
 * @param {*} searchKey 
 * @param {*} current 
 * @param {*} size 
 * @returns 
 */
function getTaskSearchPage(searchKey, current = 1, size = 10) {
  return request.get(BASE_URL + '/search/page/list?searchKey='+ searchKey, { 
    current, size 
  });
};

/**
 * 任务统计数据
 * @param {*}  taskId 任务id
 */
function taskStatistics(taskId) {
  return request.get(BASE_URL + '/' + taskId + '/statistics');
}

/**
 * 分页获取任务的所有打卡记录列表(包括finish和overdue)
 * @param {*} taskId 
 * @param {*} current 
 * @param {*} size 
 * @returns 
 */
function taskCheckInRecordPage(taskId, current = 1, size = 10) {
  return request.get(BASE_URL + '/'+ taskId +'/check-in/page/list', { 
    current, size 
  });
};

/**
 * 获取任务的设置
 * @param {*}  userTaskId 我的任务id
 */
function getUserTaskSetting(userTaskId) {
  return request.get(BASE_URL + '/user/task/' + userTaskId + '/setting');
}

/**
 * 保存任务的设置
 * @param {*}  userTaskId 我的任务id
 * @param {*} param 用户任务设置提交的参数
 */
function saveUserTaskSetting(userTaskId, param) {
  return request.post(BASE_URL + '/user/task/' + userTaskId + '/setting', param);
}

module.exports = {
  joinTask,
  getTaskPage,
  getSquare,
  taskDetail,
  quitTask,
  delTask,
  getUserTaskPage,
  publishTask,
  editTask,
  getCycleRuleAll,
  getTaskMemberPage,
  userTaskDetail,
  userTaskCheckIn,
  getUserTaskCheckInPage,
  getUserTaskCheckInPageListForDate,
  earlyFinishTask,
  getTaskSearchPage,
  taskStatistics,
  taskCheckInRecordPage,
  getUserTaskSetting,
  saveUserTaskSetting
} 